using System;
using System.Text;
using System.Threading;
using System.Collections.Generic;


namespace Foundations.Net.Pooling
{
    
    public static class Scheduler
    {

        static Int32 count = 0;
     
        static Scheduler()
        {
            PoolManager.CreatePool<Worker>(64,new ConstructorDelegate<Worker>(Construct), new ActivationDelegate <Worker>(Activate));
        }

        public static void Schedule()
        {
            PooledObject<Worker> obj = PoolManager.Get<Worker>();
            using (obj)
            {
                obj.Instance.DoWork();
            }
        }

        static Worker Construct()
        {
           Worker worker = new Worker(count);
           Interlocked.Increment(ref count);
           return worker;
        }

        static void Activate(Worker worker)
        {
            worker.ThreadId = Thread.CurrentThread.Name;
        }

    }
}
